<?php 
//php console head
//require_once('C:\wamp\www\test\PhpConsole\PhpConsole.php');
//PhpConsole::start();
// test
//debug('php console debug enbled');

//lagger head
//require_once ('C:\wamp\www\test\lagger\examples\config.php');
//require_once ('C:\wamp\www\test\lagger\examples\lagger_init.php');


require_once('C:\wamp\www\test\FirePHPCore\FirePHP.class.php');
ob_start();

$firephp = FirePHP::getInstance(true);
$firephp->log('$$$$$$$$$$$$Debug in gridjsonfetch.php'); 




$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction

if(!$sidx)$sidx=1;

$username = "admin";
$password = "admin";
$database = "staff_db";
$server = "localhost";

$con_staff=mysql_connect($server,$username,$password)or die("Connection Error: ".mysql_error());


mysql_select_db($database) or die("Error connecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM persons");

$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count =$row['count'];

if($count>0){
	$total_pages=ceil($count/$limit);
}else{
	$total_pages=0;
}

if($page>$total_pages)$page=$total_pages;
$start =$limit*$page -$limit;

$SQL ="SELECT * from persons ORDER BY $sidx $sord LIMIT $start, $limit";  

$result =mysql_query($SQL) ;//or die(writeToFile("Counld not execute query.".mysql_error()));

$responce->total=$total_pages;
$responce->page=$page;
$responce->records=$count;
$i=0;

while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
	$responce->rows[$i]['PersonID']=$i;//$row['PersonID'];
	$responce->rows[$i]['cell']=array($row['PersonID'],$row['FirstName'],$row['LastName'],$row['Age']);
	$i++;
	
	
};

echo json_encode($responce);
?>
